#include "linuxheader.h"

#include <workflow/WFHttpServer.h>
static WFFacilities::WaitGroup waitGrop(1);

void sigHandler(int num){

fprintf(stderr,"grop is done");
waitGrop.done();
}
void process(WFHttpTask *serverTask)
{
    // 设置响应的首行
    auto resp = serverTask->get_resp();
    resp->set_http_version("HTTP/1.1");
    resp->set_status_code("200");
    resp->set_reason_phrase("OK");
    resp->set_header_pair("Content-Type", "text/plain");
    resp->append_output_body("hello");
    serverTask->set_callback([](WFHttpTask *serverTask)
                             { fprintf(stderr, "serverTask Callback is runing!\n"); });
    fprintf(stderr, "process is runing\n");

    auto req = serverTask->get_req();
    fprintf(stderr, "%s %s %s", req->get_method(),
            req->get_request_uri(),
            req->get_http_version());
            fprintf(stderr,"\n");
    protocol::HttpHeaderCursor cursor(req);
    std::string name;
    std::string value;
while(cursor.next(name,value))
{
        fprintf(stderr, "%s:%s\n", name.c_str(), value.c_str());
}
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
if(addr.sin_family==AF_INET)
{
    fprintf(stderr,"sin_family:AF_INET\n");
    fprintf(stderr,"ip:%s\n",inet_ntoa(addr.sin_addr));
    fprintf(stderr,"port:%d\n",ntohs(addr.sin_port));
}
}
int main()
{
    //创建一个服务端也需要先gropwait
   signal(SIGINT,sigHandler);
  
    WFHttpServer server(process);
    if(server.start(1234)==0)
    {
        waitGrop.wait();
        server.stop();
    }
    else{
        perror("server start failed\n");
        return -1;
    }
    return 0;
}